package com.capt_3;

import java.util.Random;

public class Exercise {
    private Equation[] equations;
    public Exercise(){
        equations = new Equation[50];
    }
    // 判断习题集是否重复
    public boolean isEquationSam(Equation e,int index){
        boolean flag = false;
        for(int i=0;i<index;i++){
            if (e.isEqual(equations[i])){
                flag = true;
                break;
            }
        }
        return flag;
    }

//     产生加法
    public Equation getddEquation(){
        Equation equation = new Equation();
        int count = 0;
        do {
            equation.setLeftNumber(equation.getLeftNumbers(100,0));
            equation.setRightNumber(equation.getLeftNumbers(100,0));
            equation.setOp('+');
            equation.add();
            count ++;
        }while (!(equation.isBetween(equation.getResult(),100,0)));{
            System.out.println("获取加法尝试多少"+count+"次");
        }
        return equation;
    }
    //     产生-法
    public Equation getsubEquation(){
        Equation equation = new Equation();
        int count = 0;
        do {
            equation.setLeftNumber(equation.getLeftNumbers(100,0));
            equation.setRightNumber(equation.getLeftNumbers(100,0));
            equation.setOp('-');
            equation.sub();
            count ++;
        }while (!(equation.isBetween(equation.getResult(),100,0)));{
            System.out.println("获取-法尝试多少"+count+"次");
        }
        return equation;
    }


//    产生随机算法
    public Equation getRandomEquation(){
        Random random = new Random();
        if (random.nextInt(2)== 0){
            return getddEquation();
        }else {
            return getsubEquation();
        }
    }


//    加法习题集  产生50个,将其添加到数组中
    public void generateAddExercise(){
        for (int i=0;i<50;){
            Equation equation = getddEquation();
//            判断产生的算式是否重复
            if (isEquationSam(equation,i)){
//                 如果重复,重新获取新的算式,i不能自增
                continue;
            } else {
                equations[i] = equation;
                i++;
            }
        }

    }

//    随机习题集
    public void generateSubExercise(int num){
        equations = new Equation[num];
        int count = 0;
        while (count < num){
            Equation e1 = getsubEquation();
            if (isEquationSam(e1,count)){
    //                 如果重复,重新获取新的算式,i不能自增
                continue;
            } else {
                equations[count] = e1;
                count++;
            }
        }

    }

    public void printExercise(){
        for (int i=0; i<equations.length;i++){
//            System.out.println(equations[i].toString());
            System.out.println("第"+(i+1)+"题"+equations[i]);
        }
    }

}
